<%@ page import="java.lang.Runnable"%>
<%@ page import="java.lang.Thread"%>
<%@ page import="net.tinyos.tools.ListenLuoSensor"%>
<%@ page import="edu.zju.PortFinder"%>
<%@ page import="java.util.Map"%>
<%@ page import="edu.zju.ListenThread"%>
<%@ page import="edu.zju.ThreadManager"%>
<%
	String realPath = application.getRealPath("/");
	PortFinder finder = new PortFinder(realPath);
	int id = Integer.parseInt(request.getParameter("nodeid")) + 1; //FIXME:+1 here or +1 at js

	Map<String, String> map = finder.find();
	System.out.println("map:" + map);
	String usbPort = map.get(id + "");

	if (usbPort == null || usbPort.equals("")) {
		//return ajax info 
	} else {
		System.out.println("Begining listen node " + id + "@ port "
				+ usbPort);

		//first kill all listener thread
		ThreadManager mnger = ThreadManager.newInstance();
		mnger.killAllListener();

		String str1 = "serial@/dev/ttyUSB" + usbPort + ":telosb";
		ListenThread t = new ListenThread("-comm", str1);
		Thread listener = new Thread(t);
		mnger.putListener(usbPort, listener);
		listener.start();

		out.println("success");
	}
%>
